<?php
/**
 * 功能描述：	通用文章管理
 * @datetime	2022-07-12
 * @version   	v2.3
 * @author   	ZHL
 * @copyrigh  	2022 ZHL 版权所有
 */

class Admin_EssayServe extends Model {

    /**
     * Notes:分页列表-通用主附表关联
     * User: ZhuHaili
     */
    function getEssayList($mouldCode, $menu_id, $pageNo, $fid, $search, $orderby){
        // 参数验证
        if(!$mouldCode){
            return '出错了！请刷新浏览器重试';
        }
        if(!$menu_id){
            return '出错了！请刷新浏览器重试';
        }
        // 查询条件
        $searchInfo = SYS_SESSIONS_GET($mouldCode.'_search_info');
        $whereAdd = $searchInfo ? $searchInfo : array();
        if(!empty($search) && is_array($search)){
            $whereAdd = array();
            foreach($search as $k=>$v){
                if($k == 'search_title' && $v){
                    $whereAdd['locate']['a.title'] = $v;
                }else if($k == 'search_status' && $v){
                    $whereAdd['where']['a.status'] = $v;
                }else if($k == 'search_startdate' && $v){
                    $whereAdd['gts']['a.createdate'] = $v;
                }else if($k == 'search_enddate' && $v){
                    $whereAdd['lts']['a.createdate'] = $v;
                }
            }
            SYS_SESSIONS_SET($mouldCode.'_search_info', $whereAdd);
        }
        $essayM = new Admin_EssayModel();
        // 分页条数
        $limit = 20;
        if(isset($GLOBALS['ADMIN']['sys_pages']) && intval($GLOBALS['ADMIN']['sys_pages'])){
            $limit = intval($GLOBALS['ADMIN']['sys_pages']);
        }
        // 分页计算
        $counts = $essayM->gerEssayCount($mouldCode, $menu_id, $fid, $whereAdd);
        $pageSum = $counts ? ceil($counts/$limit) : 1;    // 总页数
        $pageSum = $pageSum > 1 ? $pageSum : 1;
        $pageNo = $pageNo > $pageSum ? $pageSum : $pageNo; // 最大页数==总页数
        $start = $pageNo * $limit - $limit;
        $pages['counts'] = $counts;
        $pages['pageNo'] = $pageNo;
        $pages['pageSum'] = $pageSum;
        // 获取列表数据
        $list = $essayM->getEssayList($mouldCode, $menu_id, $fid, $whereAdd, $start, $limit, $orderby);
        $statusArr = BaseData::$statusArr2;
        foreach($list as $k=>$v){
            $list[$k]['statustxt'] = $statusArr[$v['status']];
        }
        $result['list'] = $list;
        $result['pages'] = $pages;
        $result['where'] = $whereAdd;
        return $result;
    }

    /**
     * Notes:列表导出-通用主附表关联
     * User: ZhuHaili
     */
    function getEssayLists($mouldCode, $menu_id, $pageNo, $fid, $idStr, $search, $orderby){
        // 查询条件
        $searchInfo = SYS_SESSIONS_GET($mouldCode.'_search_info');
        $whereAdd = $searchInfo ? $searchInfo : array();
        if(!empty($search) && is_array($search)){
            $whereAdd = array();
            foreach($search as $k=>$v){
                if($k == 'search_title' && $v){
                    $whereAdd['locate']['a.title'] = $v;
                }else if($k == 'search_status' && $v){
                    $whereAdd['where']['a.status'] = $v;
                }else if($k == 'search_startdate' && $v){
                    $whereAdd['gts']['a.createdate'] = $v;
                }else if($k == 'search_enddate' && $v){
                    $whereAdd['lts']['a.createdate'] = $v;
                }
            }
            SYS_SESSIONS_SET($mouldCode.'_search_info', $whereAdd);
        }
        if($idStr){
            $whereAdd['in']['a.id'] = $idStr;
        }
        $essayM = new Admin_EssayModel();
        $start = 0;
        $limit = 0;
        if($pageNo){
            // 分页条数
            $limit = 20;
            if(isset($GLOBALS['ADMIN']['sys_pages']) && intval($GLOBALS['ADMIN']['sys_pages'])){
                $limit = intval($GLOBALS['ADMIN']['sys_pages']);
            }
            // 计算分页
            $counts = $essayM->gerEssayCount($mouldCode, $menu_id, $fid, $whereAdd);
            $pageSum = $counts ? ceil($counts/$limit) : 1;    // 总页数
            $pageSum = $pageSum > 1 ? $pageSum : 1;
            $pageNo = $pageNo > $pageSum ? $pageSum : $pageNo; // 最大页数==总页数
            $start = $pageNo * $limit - $limit;
        }

        // 获取自定义的字段标识和注释名字
        $fieldSer = new Admin_FieldServe();
        $fields1 = $fieldSer->getOpenClassFieldsInfos($menu_id, '2', '1');
        $fields = $fields1;
        //$fields2 = $fieldSer->getOpenClassFieldsInfos($menu_id, '2', '2');
        //$fields = array_merge($fields1, $fields2);

        $thList['title'] = '标题';
        $thList['status'] = '状态';
        $thList['statustxt'] = '状态描述';
        $thList['createdate'] = '创建时间';
        if(!empty($fields)){
            foreach($fields as $f){
                $thList[$f['fieldcode']] = $f['fieldname'];
            }
        }
        $essayM = new Admin_EssayModel();
        $trList = $essayM->getEssayLists($mouldCode, $menu_id, $fid, $whereAdd, $start, $limit, $orderby);
        $statusArr = BaseData::$statusArr;
        foreach($trList as $k=>$v){
            $trList[$k] = $v;
            $trList[$k]['statustxt'] = isset($statusArr[$v['status']]) ? $statusArr[$v['status']] : $v['status'];
        }
        return array('thList'=>$thList,'trList'=>$trList, 'page'=>$pageNo);
    }

    /**
     * Notes:删除
     * User: ZHL
     */
    function delEssay($mould, $menu_id, $id){
        $id = intval($id);
        $menu_id = intval($menu_id);
        if(!$mould){return '出错了，请刷新浏览器重试';}
        if(!$menu_id){return '出错了，请刷新浏览器重试';}
        if(!$id){return '没有找到要删除的内容';}

        $db = $this->conn('mysql');
        $where['id'] = $id;
        $where['menu_id'] = $menu_id;
        $where['id'] = $id;
        $result =  $db->delData($mould, $where);
        if(!$result){return '删除失败';}

        $whereDetail['essay_id'] = $id;
        $result =  $db->delData($mould.'_detail', $whereDetail);

        $mouldInfo = $db->findData('sysmould', array('mouldcode'=>$mould));
        if(isset($mouldInfo['classflag']) && $mouldInfo['classflag'] == '1'){
            $db->delData($mould.'_class', $whereDetail);
        }
        if(isset($mouldInfo['tmplflag']) && $mouldInfo['tmplflag'] == '1') {
            $db->delData($mould . '_list', $whereDetail);
        }
        return $result ? 'Y' : '详情删除失败';
    }

    /**
     * Notes:批量删除
     * User: ZHL
     */
    function delEssayMore($mould, $menu_id, $idStr){
        $menu_id = intval($menu_id);
        if(!$mould){return '出错了，请刷新浏览器重试';}
        if(!$menu_id){return '出错了，请刷新浏览器重试';}
        if(!$idStr){return '没有找到要删除的内容';}
        //$idArr = explode(',', $idStr);
        $where = " and id in(0,$idStr) and menu_id=$menu_id ";
        $db = $this->conn('mysql');
        $result = $db->delData($mould, $where);
        if(!$result){return '删除失败';}

        $whereDetail = " and essay_id in($idStr) ";
        $result = $db->delData($mould.'_detail', $whereDetail);

        $mouldInfo = $db->findData('sysmould', array('mouldcode'=>$mould));
        if(isset($mouldInfo['classflag']) && $mouldInfo['classflag'] == '1'){
            $db->delData($mould.'_class', $whereDetail);
        }
        if(isset($mouldInfo['tmplflag']) && $mouldInfo['tmplflag'] == '1') {
            $db->delData($mould . '_list', $whereDetail);
        }

        return $result ? 'Y' : '详情删除失败';
    }

    /**
     * Notes:批量移动
     * User: ZHL
     */
    function moveEssayMore($mould, $menu_id, $newClassId, $idStr){
        $menu_id = intval($menu_id);
        if(!$mould){return '出错了，请刷新浏览器重试';}
        if(!$menu_id){return '出错了，请刷新浏览器重试';}
        if(!$idStr){return '没有找到要操作的内容';}
        //$idArr = explode(',', $idStr);
        $where = " and id in(0,$idStr) and menu_id=$menu_id ";
        $data['menu_id'] = $newClassId;
        $result = $this->conn('mysql')->updateData($mould, $data, $where);
        if(!$result){return '操作失败';}
        return 'Y';
    }

    /**
     * Notes: 排序
     * User: ZhuHaili
     * Date: 2022/7/18
     * @param $param
     * @return string
     */
    function seqEssay($mould, $param){
        if(!$mould){return '排序失败';}
        if(!is_array($param)){return '排序失败';}
        foreach($param as $id=>$seq){
            $where['id'] = $id;
            $data['viewseq'] = intval($seq);
            $this->conn('mysql')->updateData($mould, $data, $where);
        }
        return 'Y';
    }

    /**
     * Notes:批量修改状态
     * User: ZhuHaili
     */
    public function updateEssayStatusMore($mould, $menu_id, $newStatus, $idStr){
        $menu_id = intval($menu_id);
        if(!$mould){return '出错了，请刷新浏览器重试';}
        if(!$menu_id){return '出错了，请刷新浏览器重试';}
        if(!$idStr){return '没有找到要操作的内容';}
        //$idArr = explode(',', $idStr);
        $where = " and id in(0,$idStr) and menu_id=$menu_id ";
        $data['status'] = $newStatus;
        $result = $this->conn('mysql')->updateData($mould, $data, $where);
        if(!$result){return '操作失败';}
        return 'Y';
    }

    /**
     * Notes:添加 / 修改
     * User: ZHL
     * Date: 2019/9/22
     * @param $param
     * @return string
     */
    function setEssay($param, $setFlag){
        // 参数验证
        if(!in_array($setFlag, array('add', 'update'))){return '非法请求_未指定操作类型';} // 类型是否add和update
        if($setFlag == 'update'){
            if(!isset($param['id']) || !intval($param['id'])){
                return '非法请求_未指文章ID';
            }
            $id = intval($param['id']);
            $commModel = new Admin_CommonModel();
            $essayInfo = $commModel->getEssayInfos($param['mould_code'], array('a.id'=>$id));
        }
        unset($param['subFlag']);
        $mouldCode = $param['mould_code'];
        $menu_id = intval($param['menu_id']);
        if(!$mouldCode){return '非法请求_未指定模型';}
        if(!$menu_id){return '非法请求_未指定菜单';}
        // 获取开放字段
        $fieldSer = new Admin_FieldServe();
        $fields1 = $fieldSer->getOpenClassFieldsInfos($menu_id, '2', '1');
        $fields2 = $fieldSer->getOpenClassFieldsInfos($menu_id, '2', '2');
        // 主表匹配插入的数据
        $data = $this->getParamMateFieldData($fields1, $param);
        $data = !empty($data) && is_array($data) ? array_filter($data) : array();
        $data['menu_id'] = $menu_id;
        $data['title'] = isset($param['title']) ? $param['title'] : '无标题';
        $data['fid'] = isset($param['fid']) ? intval($param['fid']) : 0;
        $data['status'] = isset($param['status']) ? intval($param['status']) : 1;
        $data['viewseq'] = isset($param['viewseq']) ? intval($param['viewseq']) : 0;
        $data['createdate'] = date('Y-m-d H:i:s');
        if(isset($param['createdate']) && $param['createdate']){
            $data['createdate'] = date("Y-m-d H:i:s", strtotime($param['createdate']));
        }
        // 主表数据操作
        $db = $this->conn('mysql');
        if($setFlag == 'add'){
            $id = $db->addData($mouldCode, $data, 'id');
            if(!$id){ return '添加主数据失败';}
        }else if($setFlag == 'update'){
            $mainRes = $db->updateData($mouldCode, $data, array('id'=>$id));
            if(!$mainRes){ return '修改主数据失败';}
        }
        // 匹配副表数据
        $dataDetail = $this->getParamMateFieldData($fields2, $param);
        $dataDetail = !empty($dataDetail) && is_array($dataDetail) ? array_filter($dataDetail) : array();
        $dataDetail['essay_id'] = $id;
        if($setFlag == 'add'){
            $viceResult = $db->addData($mouldCode.'_detail', $dataDetail);  // 插入副表数据
        }else if($setFlag == 'update'){
            $viceResult = $db->updateData($mouldCode.'_detail', $dataDetail, array('essay_id'=>$id));  // 修改副表数据
        }
        if(!$viceResult){ return '详情数据操作失败';}
        // 开启了多个菜单关联文章时
        if($GLOBALS['ADMIN']['classmore_flag'] == 1){
            $essay_menu_mate = isset($param['essay_menu_mate']) ? $param['essay_menu_mate'] : '';
            $this->addEssayMenuMate($mouldCode, $menu_id, $id, $essay_menu_mate,$setFlag);
        }

        // 更新文件附件日志
        $commSer = new C_CommonServe();
        $fileFields = $commSer->getParamFieldFileType(array_merge($fields1,$fields2), $param);
        foreach($fileFields as $fieldName){
            if(strpos($param[$fieldName],'|') !== false){
                foreach(explode('|', $param[$fieldName]) as $fieldOne){
                    $commSer->updateFileLogStatus($fieldOne, 1);
                }
            }else{
                $commSer->updateFileLogStatus($param[$fieldName], 1);
            }
        }
        if($setFlag == 'update'){
            foreach($fileFields as $fieldName){
                if(isset($essayInfo[$fieldName]) && $essayInfo[$fieldName]){
                    if(strpos($essayInfo[$fieldName],'|') !== false){
                        foreach(explode('|', $essayInfo[$fieldName]) as $fieldOne){
                            $commSer->updateFileLogStatus($fieldOne, 2);
                        }
                    }else {
                        $commSer->updateFileLogStatus($essayInfo[$fieldName], 2);
                    }
                }
            }
        }

        return 'Y';
    }

    /**
     * Notes:添加文章关联栏目
     * User: ZhuHaili
     * Date: 2022/9/30
     */
    private function addEssayMenuMate($mouldCode, $menu_id, $id, $essay_menu_mate,$setFlag){
        // 未传过来值时
        if(!$essay_menu_mate){
            return false;
        }
        // 验证表是否存在
        $db = $this->conn('mysql');
        $mouldSer = new Admin_MouldServe();
        $tableVerify = $mouldSer->verifyTable($db->db_fix.$mouldCode.'_class');
        if(!$tableVerify){
            return false;
        }
        // 修改时先删除旧数据
        if($setFlag == 'update'){ $db->delData($mouldCode.'_class', array('essay_id'=>$id));}
        // 默认添加一个自己菜单
        $dataClass['menu_id'] = $menu_id;
        $dataClass['essay_id'] = $id;
        $db->addData($mouldCode.'_class', $dataClass);
        // 多个栏目加入关联
        $menuIdArr = explode(',', $essay_menu_mate);
        foreach($menuIdArr as $k=>$menuIdMate){
            if($menu_id != $menuIdMate){
                $dataClass['menu_id'] = $menuIdMate;
                $db->addData($mouldCode.'_class', $dataClass);
            }
        }
        return true;
    }

    /**
     * Notes:参数匹配字段整合数据
     * User: ZhuHaili
     * Date: 2022/8/9
     */
    public function getParamMateFieldData($fields, $param){
        $data = array();
        foreach($fields as $k=>$v){
            $field = $v['fieldcode'];
            // 字段存在数组中时
            if(array_key_exists($field, $param)){
                if(is_array($param[$field])){
                    $fieldArr = array();
                    foreach($param[$field] as $key=>$val){
                        $fieldArr[$key] = $val;
                    }
                    $fieldStr = implode('|',$fieldArr);
                    $data[$field] = $fieldStr;
                }else {
                    $data[$field] = $param[$field];
                    if($v['fieldtype'] == 5 && $param[$field]){ // 密码
                        $data[$field] = setPwd($param[$field]);
                    }else if(in_array($v['fieldtype'], array(2, 13, 14, 15, 16, 17))){
                        $data[$field] = str_replace(SYS_LINK,'', $data[$field]);
                    }
                }
            }
        }
        return $data;
    }

    /**
     * Notes:获取文章关联栏目
     * User: ZHL
     */
    public function getEssayIdMenuId($mouldCode, $essayId){
        $result = array();
        $commM = new Admin_CommonModel();
        $list = $commM->getEssayList($mouldCode.'_class', array('essay_id'=>$essayId));
        if(!empty($list)){
            foreach($list as $k=>$v){
                array_push($result, $v['menu_id']);
            }
        }
        return $result;
    }

    ///////////////////////////////////////////////////////////////////////

    /**
     * Notes:文章子集
     * User: ZhuHaili
     */
    public function getEssaySideList($mouldCode, $essayId, $pageNo, $fid, $search, $orderby="viewseq desc,id asc"){
        // 参数验证
        if(!$mouldCode){
            return '出错了！请刷新浏览器重试';
        }
        $mouldCode = $mouldCode.'_side';
        if(!$essayId){
            return '出错了！请刷新浏览器重试';
        }
        // 查询条件
        $searchInfo = SYS_SESSIONS_GET($mouldCode.'_search_info');
        $whereAdd = $searchInfo ? $searchInfo : array();
        if(!empty($search) && is_array($search)){
            $whereAdd = array();
            foreach($search as $k=>$v){
                if($k == 'search_title' && $v){
                    $whereAdd['locate']['a.title'] = $v;
                }else if($k == 'search_status' && $v){
                    $whereAdd['where']['a.status'] = $v;
                }else if($k == 'search_startdate' && $v){
                    $whereAdd['gts']['a.createdate'] = $v;
                }else if($k == 'search_enddate' && $v){
                    $whereAdd['lts']['a.createdate'] = $v;
                }
            }
            SYS_SESSIONS_SET($mouldCode.'_search_info', $whereAdd);
        }

        $essayM = new Admin_CommonModel();

        // 分页条数
        $limit = 20;
        if(isset($GLOBALS['ADMIN']['sys_pages']) && intval($GLOBALS['ADMIN']['sys_pages'])){
            $limit = intval($GLOBALS['ADMIN']['sys_pages']);
        }

        $whereAdd['where']['essay_id'] = $essayId;
        $whereAdd['where']['fid'] = intval($fid);

        // 分页计算
        $counts = $essayM->getListsCount($mouldCode, $whereAdd);
        $pageSum = $counts ? ceil($counts/$limit) : 1;    // 总页数
        $pageSum = $pageSum > 1 ? $pageSum : 1;
        $pageNo = $pageNo > $pageSum ? $pageSum : $pageNo; // 最大页数==总页数
        $start = $pageNo * $limit - $limit;
        $pages['counts'] = $counts;
        $pages['pageNo'] = $pageNo;
        $pages['pageSum'] = $pageSum;
        // 获取列表数据
        $list = $essayM->getListsPage($mouldCode, $whereAdd, $start, $limit, $orderby);
        $statusArr = BaseData::$statusArr2;
        foreach($list as $k=>$v){
            $list[$k]['statustxt'] = $statusArr[$v['status']];
        }
        $result['list'] = $list;
        $result['pages'] = $pages;
        $result['where'] = $whereAdd;
        return $result;
    }

    /**
     * Notes:文章子集列表导出
     * User: ZhuHaili
     * Date: 2022/9/29
     */
    public function getEssaySideLists($mouldCode, $menu_id, $essay_id, $fid, $idStr, $search, $orderby, $pageNo){
        // 查询条件
        $searchInfo = SYS_SESSIONS_GET($mouldCode.'_side_search_info');
        $whereAdd = $searchInfo ? $searchInfo : array();
        if(!empty($search) && is_array($search)){
            $whereAdd = array();
            foreach($search as $k=>$v){
                if($k == 'search_title' && $v){
                    $whereAdd['locate']['a.title'] = $v;
                }else if($k == 'search_status' && $v){
                    $whereAdd['where']['a.status'] = $v;
                }else if($k == 'search_startdate' && $v){
                    $whereAdd['gts']['a.createdate'] = $v;
                }else if($k == 'search_enddate' && $v){
                    $whereAdd['lts']['a.createdate'] = $v;
                }
            }
            SYS_SESSIONS_SET($mouldCode.'_search_info', $whereAdd);
        }
        if($idStr){
            $whereAdd['in']['id'] = $idStr;
        }
        $whereAdd['where']['essay_id'] = $essay_id;
        $whereAdd['where']['fid'] = intval($fid);

        $essayM = new Admin_CommonModel();
        $start = 0;
        $limit = 0;
        if($pageNo){
            // 分页条数
            $limit = 20;
            if(isset($GLOBALS['ADMIN']['sys_pages']) && intval($GLOBALS['ADMIN']['sys_pages'])){
                $limit = intval($GLOBALS['ADMIN']['sys_pages']);
            }
            // 计算分页
            $counts = $essayM->getListsCount($mouldCode, $whereAdd);
            $pageSum = $counts ? ceil($counts/$limit) : 1;    // 总页数
            $pageSum = $pageSum > 1 ? $pageSum : 1;
            $pageNo = $pageNo > $pageSum ? $pageSum : $pageNo; // 最大页数==总页数
            $start = $pageNo * $limit - $limit;
        }

        // 获取自定义的字段标识和注释名字
        $fieldSer = new Admin_FieldServe();
        $fields = $fieldSer->getOpenClassFieldsInfos($menu_id, '2', '3');

        $thList['title'] = '标题';
        $thList['status'] = '状态';
        $thList['statustxt'] = '状态描述';
        $thList['createdate'] = '创建时间';
        if(!empty($fields)){
            foreach($fields as $f){
                $thList[$f['fieldcode']] = $f['fieldname'];
            }
        }
        $trList = $essayM->getListsPage($mouldCode, $whereAdd, $start, $limit, $orderby);
        $statusArr = BaseData::$statusArr;
        foreach($trList as $k=>$v){
            $trList[$k] = $v;
            $trList[$k]['statustxt'] = isset($statusArr[$v['status']]) ? $statusArr[$v['status']] : $v['status'];
        }
        return array('thList'=>$thList,'trList'=>$trList, 'page'=>$pageNo);
    }

    /**
     * Notes:子集添加 / 修改
     * User: ZHL
     * Date: 2019/9/22
     * @param $param
     * @return string
     */
    function setEssaySide($param, $setFlag){
        // 参数验证
        if(!in_array($setFlag, array('add', 'update'))){return '非法请求_未指定操作类型';} // 类型是否add和update
        if($setFlag == 'update'){
            if(!isset($param['id']) || !intval($param['id'])){
                return '非法请求_未指文章ID';
            }
            $id = intval($param['id']);
            $commModel = new Admin_CommonModel();
            $essayInfo = $commModel->getEssayInfo($param['mould_code'].'_side', array('id'=>$id));
            if(empty($essayInfo)){
                return '失败';
            }
        }
        unset($param['subFlag']);
        $mouldCode = $param['mould_code'];
        $menu_id = intval($param['menu_id']);
        if(!$mouldCode){return '非法请求_未指定模型';}
        if(!$menu_id){return '非法请求_未指定菜单';}
        // 获取开放字段
        $fieldSer = new Admin_FieldServe();
        $fields = $fieldSer->getOpenClassFieldsInfos($menu_id, '2', '3');
        // 主表匹配插入的数据
        $data = $this->getParamMateFieldData($fields, $param);
        $data = !empty($data) && is_array($data) ? $data : array();
        $data['title'] = isset($param['title']) ? $param['title'] : '无标题';
        $data['fid'] = isset($param['fid']) ? intval($param['fid']) : 0;
        $data['status'] = isset($param['status']) ? intval($param['status']) : 1;
        $data['essay_id'] = isset($param['essay_id']) ? intval($param['essay_id']) : 0;
        $data['viewseq'] = isset($param['viewseq']) ? intval($param['viewseq']) : 0;
        $data['createdate'] = date('Y-m-d H:i:s');
        $data['menu_id'] = $menu_id;
        if(isset($param['createdate']) && $param['createdate']){
            $data['createdate'] = date("Y-m-d H:i:s", strtotime($param['createdate']));
        }
        // 主表数据操作
        $db = $this->conn('mysql');
        if($setFlag == 'add'){
            $id = $db->addData($mouldCode.'_side', $data, 'id');
            if(!$id){ return '添加主数据失败';}
        }else if($setFlag == 'update'){
            $mainRes = $db->updateData($mouldCode.'_side', $data, array('id'=>$id));
            if(!$mainRes){ return '修改主数据失败';}
        }

        // 更新文件附件日志
        $commSer = new C_CommonServe();
        $fileFields = $commSer->getParamFieldFileType($fields, $param);
        foreach($fileFields as $fieldName){
            if(strpos($param[$fieldName],'|') !== false){
                foreach(explode('|', $param[$fieldName]) as $fieldOne){
                    $commSer->updateFileLogStatus($fieldOne, 1);
                }
            }else {
                $commSer->updateFileLogStatus($param[$fieldName], 1);
            }
        }
        if($setFlag == 'update'){
            foreach($fileFields as $fieldName){
                if(isset($essayInfo[$fieldName]) && $essayInfo[$fieldName]){
                    if(strpos($essayInfo[$fieldName],'|') !== false){
                        foreach(explode('|', $essayInfo[$fieldName]) as $fieldOne){
                            $commSer->updateFileLogStatus($fieldOne, 1);
                        }
                    }else {
                        $commSer->updateFileLogStatus($essayInfo[$fieldName], 2);
                    }
                }
            }
        }

        return 'Y';
    }

    /////////////////////////////////////////////////////////////////////////////////////////
    ///
    ///
    /// ////////////////////////////////////////////////////////////////////////////

    /**
     * Notes:文章属性列表
     * User: ZhuHaili
     * Date: 2022/8/11
     */
    public function getEssayElementList($mouldCode, $essayId, $essayField, $pageNo){
        // 参数验证
        $essayId = intval($essayId);
        if(!$essayId){return array();}
        if(!$essayField){return array();}
        $pageNo = intval($pageNo) ? intval($pageNo) : 1;
        $essayM = new Admin_EssayModel();
        // 分页条数
        $limit = 20;
        if(isset($GLOBALS['ADMIN']['sys_pages']) && intval($GLOBALS['ADMIN']['sys_pages'])){
            $limit = intval($GLOBALS['ADMIN']['sys_pages']);
        }
        // 计算分页
        $counts = $essayM->getEssayElementCount($mouldCode, $essayId, $essayField);
        $pageSum = $counts ? ceil($counts/$limit) : 1;    // 总页数
        $pageSum = $pageSum > 1 ? $pageSum : 1;
        $pageNo = $pageNo > $pageSum ? $pageSum : $pageNo; // 最大页数==总页数
        $start = $pageNo * $limit - $limit;
        $pages['counts'] = $counts;
        $pages['pageNo'] = $pageNo;
        $pages['pageSum'] = $pageSum;
        // 获取数据
        $list = $essayM->getEssayElementList($mouldCode, $essayId, $essayField, $start, $limit);
        if(empty($list)){return array();} // 无数据返回空
        // 数据转换
        $lists = array();
        foreach($list as $k=>$v){
            $lists[$k] = Fun::arr($v['content']);
        }
        $result['list'] = $lists;
        $result['pages'] = $pages;
        return $result;
    }

    /**
     * Notes:文章属性添加 / 修改
     * User: ZhuHaili
     * Date: 2022/8/11
     */
    public function elementSet($param, $setFlag){
        // 参数验证
        if(!in_array($setFlag, array('elementAdd', 'elementUpdate'))){return '非法请求_未指定操作类型';}
        if($setFlag == 'elementUpdate'){
            if(!isset($param['lid']) || !intval($param['lid'])){
                return '非法请求_未指文章自定义模版内容ID';
            }
            $lid = intval($param['lid']);
        }
        $mouldCode = $param['mould_code'];
        if(!$mouldCode){return '非法请求_未指定模型';}
        $essayId = $param['essay_id'];
        if(!$essayId){return '非法请求_未指定列表id';}
        $tplId = $param['tpl_id'];
        if(!$tplId){return '非法请求_未指定列表模版id';}
        $essayField = $param['essay_field'];
        if(!$essayField){return '非法请求_未指定列表模版字段';}
        $viewseq = isset($param['viewseq']) ? intval($param['viewseq']) : 0;
        unset($param['mould_code']);
        unset($param['essay_id']);
        unset($param['tpl_id']);
        unset($param['essay_field']);
        unset($param['viewseq']);
        $tmplInfoJson = Fun::json($param, 320);
        $data['essay_id'] = $essayId;
        $data['tpl_id'] = $tplId;
        $data['essay_field'] = $essayField;
        $data['content'] = $tmplInfoJson;
        $data['viewseq'] = $viewseq;
        $data['createdate'] = date('Y-m-d H:i:s');
        $db = $this->conn('mysql');
        if($setFlag == 'elementAdd'){
            $result = $db->addData($data);
        }else if($setFlag == 'elementUpdate'){
            $result = $db->updateData($data, array('lid'=>$lid));
        }
        return $result ? 'Y' : '操作失败';
    }

    /**
     * Notes: 关联栏目查询文章列表
     * User: ZHL
     * Date: 2020/04/06
     */
    public function getClassidEssayList($mould, $classid){
        if(!$classid){
            return array();
        }
        $sql['field'] = 'a.*';
        $sql['table'] = $mould;
        $sql['as'] = 'a';
        $sql['leftjoin'][$mould.'_class'] = 'b on a.id=b.essay_id';
        $sql['where']['b.menu_id'] = $classid;
        $result = $this->conn('mysql')->select($sql);
        return $result;
    }

    /**
     * Notes: 栏目查询文章列表
     * User: ZHL
     * Date: 2020/04/06
     */
    public function getClassEssayList($mould, $classid){
        $sql['where']['menu_id'] = $classid;
        $sql['field'] = 'id,menu_id';
        $sql['table'] = $mould;
        return $this->conn('mysql')->select($sql);
    }

}